﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TradeMeSDK;

namespace TradeMo
{
	public class CriteriaBikesViewModel : BaseViewModel
	{
		private SearchMotorBikesCriteria _criteria;
		public SearchMotorBikesCriteria Criteria
		{
			get 
			{
				if (_criteria == null)
				{
					_criteria = new SearchMotorBikesCriteria();
					UpdateFields();
				}
				return _criteria; 
			}
			set
			{
				_criteria = value;
				UpdateFields();
			}
		}

		private Locality _selectedRegion;
		public Locality SelectedRegion
		{
			get { return _selectedRegion; }
			set
			{
				_selectedRegion = value;
				NotifyPropertyChanged("RegionName");
				Criteria.Region = _selectedRegion != null ? _selectedRegion.LocalityId : 0;
			}
		}

		public string RegionName
		{
			get
			{
				return SelectedRegion == null ? "New Zealand" : SelectedRegion.Name;
			}
		}

		public List<SelectorItem<Locality>> Regions
		{
			get
			{
				if (!App.TradeMe.Catalog.IsDownloaded) return null;

				var list = new List<SelectorItem<Locality>>();
				list.Add(new SelectorItem<Locality> { Title = "New Zealand" });
				list.AddRange(
					App.TradeMe.Catalog.Areas.Select(area => new SelectorItem<Locality> { Title = area.Name, Item = area }));
				return list.ToList();
			}
		}


		////////////////////////////////////////////////////////////////////////////

		private SelectorItem<string> _selectedSortBy;
		public SelectorItem<string> SelectedSortBy
		{
			get
			{
				return _selectedSortBy ?? (_selectedSortBy = new SelectorItem<string> { Title = "Default" });
			}
			set
			{
				_selectedSortBy = value;
				NotifyPropertyChanged("SortByName");
				Criteria.SortOrder = _selectedSortBy != null ? _selectedSortBy.Item : null;
			}
		}
		public string SortByName
		{
			get { return SelectedSortBy.Title; }
			set { SelectedSortBy.Title = value; }
		}

		public string SortByItem
		{
			get { return Criteria.SortOrder; }
			set { Criteria.SortOrder = value; }
		}

		public List<SelectorItem<string>> SortByItems
		{
			get
			{
				List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Title = "Title", Item = "TitleAsc"},
                        new SelectorItem<string> {Title = "Closing Soon", Item = "ExpiryAsc"},
                        new SelectorItem<string> {Title = "Latest Listings", Item = "ExpiryDesc"},
                        new SelectorItem<string> {Title = "Lowest Price", Item = "PriceAsc"},
                        new SelectorItem<string> {Title = "Highest Price", Item = "PriceDesc"},
                        new SelectorItem<string> {Title = "Most Bids", Item = "BidsMost"},
                        new SelectorItem<string> {Title = "Lowest Buy Now", Item = "BuyNowAsc"},
                        new SelectorItem<string> {Title = "Highest Buy Now", Item = "BuyNowDes"},
                    };

				return list;
			}
		}

		/////////////////////////////////////////////////////////////////////

		private SelectorItem<string> _selectedType;
		public SelectorItem<string> SelectedType
		{
			get
			{
				return _selectedType ?? (_selectedType = new SelectorItem<string> { Title = "All" });
			}
			set
			{
				_selectedType = value;
				NotifyPropertyChanged("TypeName");
				Criteria.Type = _selectedType != null ? _selectedType.Item : null;
			}
		}
		public string TypeName
		{
			get { return SelectedType.Title; }
			set { SelectedType.Title = value; }
		}
		public string TypeItem
		{
			get { return Criteria.Type; }
			set { Criteria.Type = value; }
		}

		public List<SelectorItem<string>> TypeItems
		{
			get
			{
				List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Title = "All", Item = "Default"},
                        new SelectorItem<string> {Title = "Classic & vintage", Item = "Classic"},
                        new SelectorItem<string> {Title = "Cruiser", Item = "Crusier"},
                        new SelectorItem<string> {Title = "Dirt bikes", Item = "Dirt"},
                        new SelectorItem<string> {Title = "Dual purpose", Item = "Dual"},
                        new SelectorItem<string> {Title = "Pocket & pit bikes", Item = "Pocket"},
                        new SelectorItem<string> {Title = "Quads & ATVs", Item = "Quad"},
                        new SelectorItem<string> {Title = "Scooters", Item = "Scooters"},
                        new SelectorItem<string> {Title = "Sports", Item = "Sports"},
                        new SelectorItem<string> {Title = "Tourers", Item = "Tourers"},
                    };

				return list;
			}
		}

		/////////////////////////////////////////////////////////////////////

		private SelectorItem<string> _selectedMake;
		public SelectorItem<string> SelectedMake
		{
			get
			{
				return _selectedMake ?? (_selectedMake = new SelectorItem<string> { Title = "Any make" });
			}
			set
			{
				_selectedMake = value;
				NotifyPropertyChanged("MakeName");
				Criteria.Make = _selectedMake != null ? _selectedMake.Item : null;
			}
		}
		public string MakeName
		{
			get { return SelectedMake.Title; }
			set { SelectedMake.Title = value; }
		}
		public string MakeItem
		{
			get { return Criteria.Make; }
			set { Criteria.Make = value; }
		}

		public List<SelectorItem<string>> MakeItems
		{
			get
			{
				List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="", Title = "Any make"},
                        new SelectorItem<string> {Item="Aprilia", Title = "Aprilia"},
                        new SelectorItem<string> {Item="BMW", Title = "BMW"},
                        new SelectorItem<string> {Item="BSA", Title = "BSA"},
                        new SelectorItem<string> {Item="Buell", Title = "Buell"},
                        new SelectorItem<string> {Item="Cagiva", Title = "Cagiva"},
                        new SelectorItem<string> {Item="Can-Am", Title = "Can-Am"},
                        new SelectorItem<string> {Item="Ducati", Title = "Ducati"},
                        new SelectorItem<string> {Item="Gas-Gas", Title = "Gas-Gas"},
                        new SelectorItem<string> {Item="Harley Davidson", Title = "Harley Davidson"},
                        new SelectorItem<string> {Item="Honda", Title = "Honda"},
                        new SelectorItem<string> {Item="Husaberg", Title = "Husaberg"},
                        new SelectorItem<string> {Item="Husqvarna", Title = "Husqvarna"},
                        new SelectorItem<string> {Item="Hyosung", Title = "Hyosung"},
                        new SelectorItem<string> {Item="Kawasaki", Title = "Kawasaki"},
                        new SelectorItem<string> {Item="KTM", Title = "KTM"},
                        new SelectorItem<string> {Item="Lifan", Title = "Lifan"},
                        new SelectorItem<string> {Item="Moto-Guzzi", Title = "Moto-Guzzi"},
                        new SelectorItem<string> {Item="MV Agusta", Title = "MV Agusta"},
                        new SelectorItem<string> {Item="Norton", Title = "Norton"},
                        new SelectorItem<string> {Item="PGO", Title = "PGO"},
                        new SelectorItem<string> {Item="Piaggio", Title = "Piaggio"},
                        new SelectorItem<string> {Item="Polaris", Title = "Polaris"},
                        new SelectorItem<string> {Item="Rhino", Title = "Rhino"},
                        new SelectorItem<string> {Item="Suzuki", Title = "Suzuki"},
                        new SelectorItem<string> {Item="Triumph", Title = "Triumph"},
                        new SelectorItem<string> {Item="Vespa", Title = "Vespa"},
                        new SelectorItem<string> {Item="Yamaha", Title = "Yamaha"},
                    };

				return list;
			}
		}

		/////////////////////////////////////////////////////////////////////

		private SelectorItem<string> _selectedEngineSizeFrom;
		public SelectorItem<string> SelectedEngineSizeFrom
		{
			get
			{
				return _selectedEngineSizeFrom ?? (_selectedEngineSizeFrom = new SelectorItem<string> { Title = "Any" });
			}
			set
			{
				_selectedEngineSizeFrom = value;
				NotifyPropertyChanged("EngineSizeFromName");
				Criteria.MinEngineSize = _selectedEngineSizeFrom != null ? Convert.ToInt32(_selectedEngineSizeFrom.Item) : 0;
			}
		}
		public string EngineSizeFromName
		{
			get { return SelectedEngineSizeFrom.Title; }
			set { SelectedEngineSizeFrom.Title = value; }
		}

		private SelectorItem<string> _selectedEngineSizeTo;
		public SelectorItem<string> SelectedEngineSizeTo
		{
			get
			{
				return _selectedEngineSizeTo ?? (_selectedEngineSizeTo = new SelectorItem<string> { Title = "Any" });
			}
			set
			{
				_selectedEngineSizeTo = value;
				NotifyPropertyChanged("EngineSizeToName");
				Criteria.MaxEngineSize = _selectedEngineSizeTo != null ? Convert.ToInt32(_selectedEngineSizeTo.Item) : 0;
			}
		}
		public string EngineSizeToName
		{
			get { return SelectedEngineSizeTo.Title; }
			set { SelectedEngineSizeTo.Title = value; }
		}

		public List<SelectorItem<string>> EngineSizeItems
		{
			get
			{
				List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="0", Title = "Any"},
                        new SelectorItem<string> {Item="50", Title = "50"},
                        new SelectorItem<string> {Item="125", Title = "125"},
						new SelectorItem<string> {Item="250", Title = "250"},
                        new SelectorItem<string> {Item="500", Title = "500"},
                        new SelectorItem<string> {Item="750", Title = "750"},
                        new SelectorItem<string> {Item="1000", Title = "1000"},
                        new SelectorItem<string> {Item="1200", Title = "1200"},
                        new SelectorItem<string> {Item="1500", Title = "1500+"},
                    };

				return list;
			}
		}


		/////////////////////////////////////////////////////////////////////

		private SelectorItem<string> _selectedPriceFrom;
		public SelectorItem<string> SelectedPriceFrom
		{
			get
			{
				return _selectedPriceFrom ?? (_selectedPriceFrom = new SelectorItem<string> { Title = "Any" });
			}
			set
			{
				_selectedPriceFrom = value;
				NotifyPropertyChanged("PriceFromName");
				Criteria.MinPrice = _selectedPriceFrom.Item != null ? Convert.ToInt32(_selectedPriceFrom.Item) : 0;
			}
		}
		public string PriceFromName
		{
			get { return SelectedPriceFrom.Title; }
			set { SelectedPriceFrom.Title = value; }
		}

		private SelectorItem<string> _selectedPriceTo;
		public SelectorItem<string> SelectedPriceTo
		{
			get
			{
				return _selectedPriceTo ?? (_selectedPriceTo = new SelectorItem<string> { Title = "Any" });
			}
			set
			{
				_selectedPriceTo = value;
				NotifyPropertyChanged("PriceToName");
				Criteria.MaxPrice = _selectedPriceTo != null ? Convert.ToInt32(_selectedPriceTo.Item) : 0;
			}
		}
		public string PriceToName
		{
			get { return SelectedPriceTo.Title; }
			set { SelectedPriceTo.Title = value; }
		}

		public List<SelectorItem<string>> PriceItems
		{
			get
			{
				List<SelectorItem<string>> list = new List<SelectorItem<string>>
                    {
                        new SelectorItem<string> {Item="0", Title = "Any"},
                        new SelectorItem<string> {Item="500", Title = "$500"},
                        new SelectorItem<string> {Item="1000", Title = "$1,000"},
						new SelectorItem<string> {Item="2000", Title = "$2,000"},
                        new SelectorItem<string> {Item="3000", Title = "$3,000"},
                        new SelectorItem<string> {Item="4000", Title = "$4,000"},
                        new SelectorItem<string> {Item="5000", Title = "$5,000"},
                        new SelectorItem<string> {Item="7500", Title = "$7,500"},
                        new SelectorItem<string> {Item="10000", Title = "$10,000"},
                        new SelectorItem<string> {Item="12500", Title = "$12,500"},
                        new SelectorItem<string> {Item="15000", Title = "$15,000"},
                        new SelectorItem<string> {Item="20000", Title = "$20,000"},
                        new SelectorItem<string> {Item="25000", Title = "$25,000"},
                        new SelectorItem<string> {Item="30000", Title = "$30,000"},
                    };

				return list;
			}
		}

		/////////////////////////////////////////////////////////////////////



		public void UpdateFields()
		{
			NotifyPropertyChanged("Criteria");

			if (Criteria.Region != 0)
				SelectedRegion = App.TradeMe.Catalog.Areas.FirstOrDefault(l => l.LocalityId == Criteria.Region);

			if (!string.IsNullOrEmpty(Criteria.SortOrder))
				SelectedSortBy = SortByItems.FirstOrDefault(s => s.Item == Criteria.SortOrder);

			if (!string.IsNullOrEmpty(Criteria.Type))
				SelectedType = TypeItems.FirstOrDefault(t => t.Item == Criteria.Type);

			if (!string.IsNullOrEmpty(Criteria.Make))
				SelectedMake = MakeItems.FirstOrDefault(m => m.Item == Criteria.Make);

			if (Criteria.MinEngineSize > 0)
				SelectedEngineSizeFrom = EngineSizeItems.FirstOrDefault(es => es.Item == Criteria.MinEngineSize.ToString());
			if (Criteria.MaxEngineSize > 0)
				SelectedEngineSizeTo = EngineSizeItems.FirstOrDefault(es => es.Item == Criteria.MaxEngineSize.ToString());

			if (Criteria.MinPrice > 0)
			{
				string price = ((int)Criteria.MinPrice).ToString();
				SelectedPriceFrom = PriceItems.FirstOrDefault(p => p.Item == price);
			}
			if (Criteria.MaxPrice > 0)
			{
				string price = ((int)Criteria.MaxPrice).ToString();
				SelectedPriceTo = PriceItems.FirstOrDefault(p => p.Item == price);
			}
		}

	    public void Reset()
	    {
            SelectedRegion = null;
            SelectedSortBy = null;
            SelectedMake = null;
            SelectedType = null;
	        SelectedEngineSizeFrom = null;
	        SelectedEngineSizeTo = null;
            SelectedPriceFrom = null;
            SelectedPriceTo = null;
            UpdateFields();
	    }
	}
}
